/*!
https://gitee.com/uqbn/labs/raw/master/unit4/main.c
*/
#include "main.h"

static void delay(uint16_t xms);

static void delay(uint16_t xms)
{
    uint16_t i, j;
    for (i = xms; i; --i)
    {
        for (j = 112U; j; --j)
        {
        }
    }
}

static uint8_t buf[3];
static uint8_t idx = 0U;

int main(void)
{
    SCON = 0xD0U;
    TMOD = 0x21U;
    PCON = 0x00U;
    TIM0_SET(0);
    TIM1_SET(0xFDFDU);
    SET_BIT(IE, UART_INT_ENABLE | TIM0_INT_ENABLE | INT_ENABLE);
    SET_BIT(TCON, TIM0_ENABLE | TIM1_ENABLE);

    for (;;)
    {
    }
}

void timer0() __interrupt(TF0_VECTOR)
{
    idx = 0;
    TIM0_SET(0);
    buf[0] = buf[1] = buf[2] = 0U;
}

void uart() __interrupt(SI0_VECTOR)
{
    buf[idx] = SBUF;
    RI       = 0U;
    ACC      = buf[idx];
#if 1 /* even check */
    if (RB8 != P)
#else
    if (RB8 == P)
#endif
    {
        return;
    }

    TB8  = P;
    SBUF = buf[idx];
    while (!TI)
    {
    }
    TI = 0U;

    if (buf[0] == 'O' && idx == 0U)
    {
        idx++;
    }
    else if (buf[1] == 'N' && idx == 1U)
    {
        idx = 0U;
        P1  = 0x00U;
    }
    else if (buf[1] == 'F' && idx == 1U)
    {
        idx++;
    }
    else if (buf[2] == 'F' && idx == 2U)
    {
        idx = 0U;
        P1  = 0xFFU;
    }
    else
    {
        idx = 0U;
    }
}

/* END OF FILE */
